package com.oa.gform.client.ui.event;

import com.google.gwt.event.shared.GwtEvent;

public class GRepeaterItemClickEvent<T> extends GwtEvent<GRepeaterItemClickHandler<T>> {
	  /**
	   * Handler type.
	   */
	  private static Type<GRepeaterItemClickHandler<?>> TYPE;

	  /**
	   * Fires a close event on all registered handlers in the handler manager. If
	   * no such handlers exist, this method will do nothing.
	   * 
	   * @param <T> the target type
	   * @param source the source of the handlers
	   * @param target the target
	   */
	  public static <T> void fire(HasGRepeaterItemClickHandler<T> source, T target) {
	    fire(source, target, "",-1);
	  }

	  /**
	   * Fires a close event on all registered handlers in the handler manager.
	   * 
	   * @param <T> the target type
	   * @param source the source of the handlers
	   * @param target the target
	   * @param autoClosed was the target closed automatically
	   */
	  public static <T> void fire(HasGRepeaterItemClickHandler<T> source, T target, String actionName,int itemIndex) {
	    if (TYPE != null) {
	    	GRepeaterItemClickEvent<T> event = new GRepeaterItemClickEvent<T>(target, actionName,itemIndex);
	      source.fireEvent(event);
	    }
	  }

	  /**
	   * Gets the type associated with this event.
	   * 
	   * @return returns the handler type
	   */
	  public static Type<GRepeaterItemClickHandler<?>> getType() {
	    return TYPE != null ? TYPE : (TYPE = new Type<GRepeaterItemClickHandler<?>>());
	  }

	  private final T target;

	  private final String actionName;
	  
	  private final int itemIndex;

	  /**
	   * Creates a new close event.
	   * 
	   * @param target the target
	   * @param autoClosed whether it is auto closed
	   */
	  protected GRepeaterItemClickEvent(T target, String actionName,int itemIndex) {
	    this.actionName = actionName;
	    this.target = target;
	    this.itemIndex = itemIndex;
	  }

	  // The instance knows its of type T, but the TYPE
	  // field itself does not, so we have to do an unsafe cast here.
	  @SuppressWarnings("unchecked")
	  @Override
	  public final Type<GRepeaterItemClickHandler<T>> getAssociatedType() {
	    return (Type) TYPE;
	  }

	  /**
	   * Gets the target.
	   * 
	   * @return the target
	   */
	  public T getTarget() {
	    return target;
	  }

	  /**
	   * Was the target automatically closed?
	   * 
	   * @return auto closed
	   */
	  public String getActionName() {
	    return actionName;
	  }

	  
	  /**
	   * Was the target automatically closed?
	   * 
	   * @return auto closed
	   */
	  public int getItemIndex() {
	    return itemIndex;
	  }

	  @Override
	  protected void dispatch(GRepeaterItemClickHandler<T> handler) {
	    handler.onClick(this);
	  }

}
